import Toast from 'toon-ui/lib/components/toast'
import imgView from '../img-view/index'
export default {
  data() {
    return {
      photoDetail: {
        'dealProcess': ''
      },
      feedContent: '',
      //showFeed: false,
      curPhotoIndex:0,    //当前放大图片
      imgSwitch:false     //图片放大开关
    }
  },
  filters: {
    thumbnail: function (url, param) {
      return url.replace(".png", param + ".png").replace(".jpg", param + ".jpg")
    },
    textLength: function (str) {
            let length = (str + "").replace(/[\r\n]/g, "占位").length;
            return length > 200 ? 200 : length;
    }
  },

  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
    let feedback ={
      index:this.$route.query.index,
      feedback:0,
      dealStatus:0,
      auditCode:0,
    };
    if(this.$util.sessionData('feedbackStatus')){
      let feedbackStatus = JSON.parse(this.$util.sessionData('feedbackStatus'));
      if(feedbackStatus.id ==  this.$route.params.id){
        this.photoDetail.feedback = feedbackStatus.feedbackStatus;
        sessionStorage.removeItem('feedbackStatus');
      }
    }
    feedback.feedback = this.photoDetail.feedback;
    feedback.dealStatus = this.photoDetail.dealStatus;
    feedback.auditCode = this.photoDetail.auditCode;
    this.$util.sessionData('feedback',JSON.stringify(feedback));
    next();
  },
  mounted() {
    this.init();
  },
  //销毁实例
  deactivated(){
    this.$destroy();
  },
  methods: {
    init: function () {
      this.$util.setTitle("问题处理");
      if (this.$util.GData("photoDetail")) {
        this.photoDetail = this.$util.GData("photoDetail");
        this.$util.GData("photoDetail", false);
      } else {
        this.queryPhotoDetailById();
      }
    },
    /**
     * 获取随手拍详情
     */
    queryPhotoDetailById: function () {
      let _this = this;
      //渲染详情页数据
      function randerData(datas) {
        // 是否有处理流程
        if (datas.dealProcess) {

        }
        //
        _this.photoDetail = datas;
        // 替换换行符
        _this.photoDetail.content = _this.photoDetail.content.replace(/\n/g, "<br>");
      }

      //如果缓存中有详情信息 则直接渲染 查看图片返回
      if (this.$util.GData("detail")) {
        return randerData(this.$util.GData("detail"))
      }

      //如果缓存中没有详情信息则请求数据
      var params = {
        "id": _this.$route.params.id
      };
      this.$http.post("/convphoto/queryPhotoDetailById", params).then(function (res) {
        if (res.meta && res.meta.code == '0'){
          var datas = res.data || {};
          randerData(datas)
        }else {
          Toast({
            'message': res.meta.message,
            'duration': 1000, // 持续时间，默认3000毫秒
          });
        }

      }).catch(function (err) {
        // Toast({
        //   'message': '获取详情失败',
        //   'duration': 1000, // 持续时间，默认3000毫秒
        // });
      })
    },
    /**
     * 用户反馈
     */
    userFeedBack: function (back) {
      let _this = this;
      //_this.feedContent = _this.feedContent.trim();
      //意见不能为空
      // if(back == 1 && _this.feedContent == ''){
      //   Toast({
      //     message: '请填写您的意见！',
      //     duration: 1000, // 持续时间，默认3000毫秒
      //     position: 'top',
      //   });
      //   return false
      // }
      var params = {
          "id": _this.$route.params.id,
          "messge": _this.feedContent,
          "userFeedBack": back
        }
        this.$http.post("/convphoto/userFeedBack", params).then(function (res) {
          if (res.meta && res.meta.code == '0'){
            _this.photoDetail.feedback = back;
            // // 隐藏反馈输入
            // _this.showFeed = false;
            // _this.feedContent = '';
          }else{
            Toast({
              'message': res.meta.message,
              'duration': 1000, // 持续时间，默认3000毫秒
            });
          }

        }).catch(function (err) {
          Toast({
            'message': '反馈失败',
            'duration': 1000, // 持续时间，默认3000毫秒
          });
        })
    },
    /**
     * 展示反馈输入框
     */
     showFeedBox(key) {
        //this.showFeed = key;
      let _this = this;
      this.$router.push({'name':'feed',params:{'id': _this.$route.params.id}})
     },

    /**
     * 跳转到查看图片
     */
    bigImg: function (index) {
      //this.$util.GData("photoDetail", this.photoDetail);
      //this.$router.push({
      //  name: 'imgView',
      //  params: {
      //    url: this.photoDetail.photos[index].fileUrl
      //  },
      //  query: {
      //    title: "问题处理"
      //  }
      //});
      this.curPhotoIndex=index;
      this.imgSwitch=true;
    },
    /**
     * 跳转专题详情页
     */
    goSubject(subjectId){
      this.$router.push({'name':'subjectList',params: { 'subjectId': subjectId }})
    },
    reset(){
      this.curPhotoIndex=-1;
    }
  },
  components: {
    imgView
  }
}
